package com.dormitory.manage.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dormitory.manage.pojo.Student;
import com.dormitory.manage.result.Result;
import com.dormitory.manage.service.StudentService;
import com.dormitory.manage.vo.StudentQueryVo;
import com.dormitory.manage.vo.StudentUpdateVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author Administrator
 * @version 1.0
 * @description StudentController
 * @date 2023/4/10 0010 20:38
 */
@RestController
@RequestMapping("student")
@CrossOrigin
@Slf4j
public class StudentController {

    @Resource
    private StudentService studentService;

    /**
    * @description 带条件分页查询学生列表
    * @param pageNo
     * @param pageSize
     * @param studentQueryVo
    * @returnType com.dormitory.manage.result.Result
    * @author Administrator
    * @date  21:20
    */
    @PostMapping("getStudentPageList/{pageNo}/{pageSize}")
    public Result getStudentPageList(@PathVariable int pageNo, @PathVariable int pageSize, @RequestBody StudentQueryVo studentQueryVo){
        Page<Student> page = new Page<>(pageNo, pageSize);
        Page<Student> studentPage = studentService.getStudentPageList(page,studentQueryVo);
        return Result.ok(studentPage);
    }

    /**
    * @description 新增学生信息
    * @param student
    * @returnType com.dormitory.manage.result.Result
    * @author Administrator
    * @date  21:22
    */
    @PostMapping("addStudent")
    public Result addStudent(@RequestBody Student student){
        boolean result = studentService.addStudent(student);
        if (result){
            return Result.ok();
        }
        return Result.fail().message("添加失败，请重试！");
    }

    /**
    * @description 更新学生信息
    * @param studentId
     * @param studentUpdateVo
    * @returnType com.dormitory.manage.result.Result
    * @author Administrator
    * @date  22:37
    */
    @PutMapping("updateStudent/{studentId}")
    public Result updateStudent(@PathVariable long studentId, @RequestBody StudentUpdateVo studentUpdateVo){
        boolean result = studentService.updateStudent(studentId,studentUpdateVo);
        if (result){
            return Result.ok();
        }
        return Result.fail().message("更新失败！");
    }

    @DeleteMapping("deleteStudent/{studentId}")
    public Result deleteStudent(@PathVariable long studentId){
        boolean result = studentService.deleteStudent(studentId);
        if (result){
            return Result.ok();
        }
        return Result.fail().message("删除失败！");
    }

}
